package com.tanhua.controller;

import com.tanhua.service.AdministratorService;
import com.tanhua.utils.CaptchaUtils;
import com.tanhua.vo.AdministratorVo;
import com.tanhua.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: cuixu
 * @Description: 用户/管理员后台controller
 * @Date: Created in 9:56 2021/1/17
 * @ModifiedBy:
 */

@RestController
@RequestMapping("system/users")
public class AdministratorController {

    @Autowired
    private AdministratorService administratorService;


    /**
     * @Description: 生成验证码图片
     * @Author: cuixu
     * @Date: 2021/1/17
     */
    @GetMapping("verification")
    public void verification(@RequestParam(name="uuid") String uuid, HttpServletResponse response, HttpServletRequest request){
        //响应头
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        CaptchaUtils captchaUtils= new CaptchaUtils();
        System.out.println(uuid);

        //生成图片
        String ranCaptcha= captchaUtils.getRandcode(request,response);
        //administratorService,传入uuid和验证码
        this.administratorService.saveVerification(uuid,ranCaptcha);
    }


    /**
     * @Description: 后台用户管理登录
     * @param:
     * @return: String token
     * @Author: cuixu
     * @Date: 2021/1/17
     */
    @PostMapping("login")
    public ResponseEntity<Object> AdministratoLogin(@RequestBody Map<String,String> param){

        try {
            String username = param.get("username");
            String password = param.get("password");
            String verificationCode = param.get("verificationCode");
            String uuid = param.get("uuid");
            String token = this.administratorService.login(username,password,verificationCode,uuid);
            if(StringUtils.isNotEmpty(token)){
                Map<String, Object> result = new HashMap<>();
                result.put("token",token);
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult.ErrorResultBuilder builder = ErrorResult.builder().errCode("000000").errMessage("登录失败");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(builder.build());

    }


    /**
     * @Description: 用户基本信息
     * @Author: cuixu
     * @Date: 2021/1/17
     */
    @PostMapping("profile")
    public ResponseEntity<AdministratorVo> profile (@RequestHeader(required = false, name = "Authorization") String token) {
        try {
            String newToken = token.replace("Bearer ", "");
            AdministratorVo administratorVo = administratorService.queryAdministratorByToken(newToken);
            if (null != administratorVo) {
                return ResponseEntity.ok(administratorVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * @Description: 登出
     * @Author: cuixu
     * @Date: 2021/1/17
     */
    @PostMapping("logout")
    private void logout(@RequestHeader("Authorization") String token){
        String newToken = token.replace("Bearer ", "");
        this.administratorService.removeToken(token);
    }

}
